package com.ptppay.cf.coin.service.btc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.ptppay.cf.btc.CFBtc;
import com.ptppay.cf.btc.ECKey;
import com.ptppay.cf.core.business.service.CFService;

import cn.finder.ui.webtool.QueryCondition;
import cn.finder.wae.business.domain.Message;
import cn.finder.wae.business.domain.TableQueryResult;
import cn.finder.wae.business.dto.MapParaQueryConditionDto;
import cn.finder.wae.common.comm.AppApplicationContextUtil;
import cn.finder.wae.queryer.handleclass.QueryerDBAfterClass;

/**
 * @author: finder
 * @function:创建 基于钱包的密钥对
 * 
 * 
 */
public class CreateWalletSPKeyAfterClass extends QueryerDBAfterClass {
	
	
	CFService cFService =AppApplicationContextUtil.getContext().getBean("cFService", CFService.class);
	
	
	private Logger logger=Logger.getLogger(CreateWalletSPKeyAfterClass.class);

	@Override
	public TableQueryResult handle(TableQueryResult tableQueryResult,
			long showTableConfigId, QueryCondition<Object[]> condition) {
		 super.handle(tableQueryResult, showTableConfigId, condition);
		 logger.info("==="+this.getClass());
		 @SuppressWarnings("unchecked")
		Map<String, Object> data = ((MapParaQueryConditionDto<String, Object>)condition).getMapParas();
			
		 List<Map<String,Object>> resultList= new ArrayList<Map<String,Object>>();
		 	
		 Map<String,Object> returnItem=new HashMap<String, Object>();
		 resultList.add(returnItem);
		tableQueryResult.setResultList(resultList);
		 	
	 	tableQueryResult.setPageIndex(condition.getPageIndex());
	 	tableQueryResult.setPageSize(condition.getPageSize());
	 	tableQueryResult.setCount(1l);
	 	
	 	
	 	
	 	String wallet_id=data.get("wallet_id").toString();
	 	
	 	
	 	if(tableQueryResult.getMessage().getResultCode()==Message.StatusCode_OK){
	 		
	 		try {
	 			
		 			
	 			CFBtc cfBtc=new CFBtc();
	 			
	 			ECKey walletData= cfBtc.createECKey(wallet_id);
	 			
	 			//save wallet to db
	 			
	 			if (walletData != null) {
	 				
	 				Map<String,Object> walletItem=new HashMap<String,Object>();
					walletItem.put("private_key", walletData.getPrivateKey());
					walletItem.put("public_key", walletData.getPublicKey());
					walletItem.put("wallet_address", walletData.getAddress());

					returnItem.putAll(walletItem);

                } else {
                    tableQueryResult.getMessage().setResultCode(Message.StatusCode_ServerError);
                    tableQueryResult.getMessage().setMsg("create eckey failed.");
                }

				
			} catch(Exception e){
		 		
		 		tableQueryResult.getMessage().setResultCode(Message.StatusCode_ServerError);
				tableQueryResult.getMessage().setMsg("find coin price failure.");
		 	}
	 		
	 	}
	 	
	 	
	 	
		return tableQueryResult;
		 
	}
	 private TableQueryResult errorReturn(TableQueryResult tableQueryResult, int resultCode, String msg) {
	        tableQueryResult.getMessage().setResultCode(resultCode);
	        tableQueryResult.getMessage().setResultMsg(msg);
	        tableQueryResult.getMessage().setMsg(msg);
	        return tableQueryResult;
	    }


	@Override
	public void setJDBCDataSource(DataSource dataSource) {
		super.setJdbcDataSource(dataSource);
	}

	
}
